package leetcode.editor.day;

import java.util.Stack;

// 946. 验证栈序列
// https://leetcode.cn/problems/validate-stack-sequences/
class ValidateStackSequences {
    public static void main(String[] args) {
        Solution solution = new ValidateStackSequences().new Solution();
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public boolean validateStackSequences(int[] pushed, int[] popped) {
            Stack<Integer> stack = new Stack<>();
            stack.push(pushed[0]);
            int i = 0, j = 0;
            while (i < pushed.length) {
                stack.push(pushed[i++]);
                while (j < popped.length && stack.peek() == popped[j]) {
                    stack.pop();
                    j++;
                }
            }
            return j == popped.length;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}
